import * as types from '../action-typess';
import { getCategory, getCateList } from '../../api/category';
import { getNine } from '../../api/home';

export function fetchCategory() {
  return function(dispatch) {
    dispatch({type: types.FETCH_CATEGORY});
    getCategory().then(payload=>{
      dispatch({type: types.FETCH_CATEGORY_FULFILLED, payload: payload.data});
    })
  }
}

export function fetchCateList(title, sortVal) {
  return function(dispatch, getState) {
    let {page, sort, searchValue} = getState().category;
    if(!title){
      title = searchValue;
     
    }
    if(!sortVal){
      sortVal = sort;
    }
    
    dispatch({type: types.FETCH_CATELIST});
    getCateList(title, sortVal, page).then(payload=>{
      payload.data.sort = sortVal;
      payload.data.searchValue = title;
      dispatch({type: types.FETCH_CATELIST_FULFILLED, payload: payload.data});
    })
  }
}

// 9块9
export function fetchNine() {
  return function(dispatch) {
    dispatch({type: types.FETCH_NINE});
    getNine().then(payload=>{
      dispatch({type: types.FETCH_NINE_FULFILLED, payload: payload.data});
    })
  }
}